class Admin::CategoryController < Admin::BaseAdminController
  before_filter :load_categories  

  def create
    @category = Category.new(params[:category])
    if request.post? && @blog.categories << @category
      redirect_to_default("Category #{@category.name} created.")
    end
  end

  def update
    @category = Category.find(params[:id])
    if request.post? && @category.update_attributes(params[:category])
      redirect_to_default("Category #{@category.name} updated.")
    end
  end

  def delete
    if request.post?
      category = Category.find(params[:id])
      if category.destroy
        redirect_to_default("Category #{category.name} destroyed.")
      end
    end
  end
  
  def push_up
    category = Category.find(params[:id])
    category.move_higher
    redirect_to_default(category.name + " moved up.")
  end
  
  def pull_down
    category = Category.find(params[:id])
    category.move_lower
    redirect_to_default(category.name + " moved down.")
  end
  
  private
  def redirect_to_default(notice = nil)
    flash[:notice] = notice unless notice.nil?
    redirect_to :action => "create"
  end
  
  def load_categories
    @categories = @blog.categories(true)
    @categories ||= []
  end
end
